package meihua.stopcarapp.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.OnClick;
import meihua.mylibrary.base.BaseActivity;
import meihua.mylibrary.baseapp.AppConfig;
import meihua.mylibrary.commonutils.ImageLoaderUtils;
import meihua.stopcarapp.R;
import meihua.stopcarapp.app.UserConfig;
import meihua.stopcarapp.bean.UserEntity;
import meihua.stopcarapp.model.UserInfoModel;
import meihua.stopcarapp.presenter.UserInfoPresenter;
import meihua.stopcarapp.presenter.contract.UserInfoContract;
import meihua.stopcarapp.utils.ToastUtil;
import meihua.stopcarapp.widget.dialog.UserInfoDialog;
import meihua.stopcarapp.widget.dialog.UserInfoSexDialog;

/**
 * 用户信息2017/5/10
 * 用户名改变，昵称随着改变，昵称没有点击事件
 */
public class UserInformationActivity extends BaseActivity<UserInfoPresenter, UserInfoModel> implements UserInfoContract.View {


    @Bind(R.id.iv_user_icon)
    ImageView mIvUserIcon;
    @Bind(R.id.tv_nickname)     //昵称：头像下面
    TextView mTvNickname;
    @Bind(R.id.tv_user_name)
    TextView mTvUserName;
    @Bind(R.id.ll_name)
    LinearLayout mLlName;
    @Bind(R.id.tv_sex)
    TextView mTvSex;
    @Bind(R.id.ll_sex)
    LinearLayout mLlSex;
    @Bind(R.id.tv_phone_number)
    TextView mTvPhoneNumber;
    @Bind(R.id.ll_phone_number)
    LinearLayout mLlPhoneNumber;
    @Bind(R.id.tv_id_card)
    TextView mTvIdCard;
    @Bind(R.id.ll_id_card)
    LinearLayout mLlIdCard;
    @Bind(R.id.tv_plate_number)
    TextView mTv_plate_number;
    @Bind(R.id.ll_plate_number)
    LinearLayout mLlPlateNumber;
    @Bind(R.id.btn_exit)
    Button mBtnExit;
    @Bind(R.id.toolbar_left)
    ImageView mIvBack;
    @Bind(R.id.toolbar_center)
    TextView mTitleName;

    String s = "1";
    private int isMan = 0;  //0男默认男 1女

    /**
     * 入口
     *
     * @param activity
     */
    public static void startAction(Activity activity) {
        Intent intent = new Intent(activity, UserInformationActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_userinformation;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mTitleName.setText("个人信息");
        mTvUserName.setText(UserConfig.USER_ENTITY.getUser_nickname());
        mTvNickname.setText(UserConfig.USER_ENTITY.getUser_nickname());
        mTvSex.setText(UserConfig.USER_ENTITY.getUser_sex());
        //加载本地图片没有效果
        ImageLoaderUtils.displayRound( this, mIvUserIcon, R.mipmap.ic_launcher_round );

    }

    @OnClick({R.id.iv_user_icon, R.id.ll_name, R.id.toolbar_left,
            R.id.ll_sex, R.id.ll_phone_number, R.id.ll_id_card, R.id.ll_plate_number,
             R.id.btn_exit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_user_icon: //更换头像

                break;
            case R.id.ll_name:   //修改昵称
                final UserInfoDialog userInfoDialogName = new UserInfoDialog(this,R.style.userinfo_sex_Dialog,
                        "修改昵称","请输入您的昵称");
                userInfoDialogName.setOnUserinfoClickListener(new UserInfoDialog.OnUserinfoClickListener() {
                    @Override
                    public void submitClick() {
                        if (userInfoDialogName.getEditText().getText().toString().equals(
                                UserConfig.USER_ENTITY.getUser_nickname())){
                            userInfoDialogName.dismiss();
                        }else {
                            mPresenter.getUserNickNameRequest(UserConfig.USER_ENTITY.getUser_phone(),
                                    UserConfig.USER_ENTITY.getUser_id(),
                                    userInfoDialogName.getEditText().getText().toString());
                            userInfoDialogName.dismiss();
                        }
                    }

                    @Override
                    public void cancleClick() {
                        userInfoDialogName.dismiss();
                    }
                });
                userInfoDialogName.show();
                break;
            case R.id.ll_sex://修改性别
                final UserInfoSexDialog userInfoSexDialog = new UserInfoSexDialog(UserInformationActivity.this,
                        R.style.userinfo_sex_Dialog);
                userInfoSexDialog.setOnSelectSexClickListener(new UserInfoSexDialog.OnSelectSexClickListener() {
                    @Override
                    public void submitClick() {
                        if (UserConfig.USER_ENTITY.getUser_sex().equals(userInfoSexDialog.getSex())){
                        }else {
                            mPresenter.getUserSexRequest(UserConfig.USER_ENTITY.getUser_phone(),
                                    userInfoSexDialog.getSex());
                        }
                        userInfoSexDialog.dismiss();
                    }

                    @Override
                    public void cancleClick() {
                        userInfoSexDialog.dismiss();
                    }
                });
                userInfoSexDialog.show();

                break;
            case R.id.ll_phone_number://修改手机号
                final UserInfoDialog userInfoDialogphone = new UserInfoDialog(this,R.style.userinfo_sex_Dialog,
                        "修改手机号","请输入您新的手机号");
                userInfoDialogphone.setOnUserinfoClickListener(new UserInfoDialog.OnUserinfoClickListener() {
                    @Override
                    public void submitClick() {
                        if (userInfoDialogphone.getEditText().getText().toString().equals(
                                UserConfig.USER_ENTITY.getUser_photo())){
                            userInfoDialogphone.dismiss();
                        }else {
                            mPresenter.getUserPhoneRequest(userInfoDialogphone.getEditText().getText().toString(),
                                    UserConfig.USER_ENTITY.getUser_id());
                            userInfoDialogphone.dismiss();
                        }
                    }

                    @Override
                    public void cancleClick() {
                        userInfoDialogphone.dismiss();
                    }
                });
                userInfoDialogphone.show();
                break;
            case R.id.ll_id_card://修改身份证号

                break;
            case R.id.ll_plate_number:  //修改车牌号
                final UserInfoDialog userInfoDialogNumber = new UserInfoDialog(this,R.style.userinfo_sex_Dialog,
                        "绑定车牌号","请输入您的车牌号");
                userInfoDialogNumber.setOnUserinfoClickListener(new UserInfoDialog.OnUserinfoClickListener() {
                    @Override
                    public void submitClick() {
                        if (userInfoDialogNumber.getEditText().getText().toString().equals(
                                UserConfig.USER_ENTITY.getUser_carid())){
                            userInfoDialogNumber.dismiss();
                        }else {
                            mPresenter.getUserPlateNumberRequest(UserConfig.USER_ENTITY.getUser_phone(),
                                    UserConfig.USER_ENTITY.getUser_id(),
                                    userInfoDialogNumber.getEditText().getText().toString());
                            userInfoDialogNumber.dismiss();
                        }
                    }

                    @Override
                    public void cancleClick() {
                        userInfoDialogNumber.dismiss();
                    }
                });
                userInfoDialogNumber.show();
                break;
            case R.id.btn_exit: //退出登录
                final UserInfoDialog userInfoDialogExit = new UserInfoDialog(this,R.style.userinfo_sex_Dialog,
                        "退出登陆","确认退出？","退出");
                userInfoDialogExit.setOnUserinfoClickListener(new UserInfoDialog.OnUserinfoClickListener() {
                    @Override
                    public void submitClick() {
                        SharedPreferences sp=getSharedPreferences("config",0);
                        SharedPreferences.Editor editor=sp.edit();
                        //把数据进行保
                        editor.putString("login_status",  AppConfig.USER_UNLOGIN);
                        editor.putString("user_id","");
                        editor.putString("user_phone","");
                        //提交数据
                        editor.commit();
                        //在内存中保存用户信息
                        UserConfig.USER_ENTITY = null;
                        userInfoDialogExit.dismiss();
                        UserInformationActivity.this.finish();
                    }

                    @Override
                    public void cancleClick() {
                        userInfoDialogExit.dismiss();
                    }
                });
                userInfoDialogExit.show();
                break;
            case R.id.toolbar_left: //退出页面
                finish();
                break;
        }
    }


    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }

    @Override
    public void returnUserNickName(String msg) {
        mTvNickname.setText( msg );
        mTvUserName.setText( msg );
    }

    @Override
    public void returnUserSex(UserEntity userEntity) {
        mTvSex.setText(userEntity.getUser_sex());
        ToastUtil.showShort(this,"修改性别成功:"+userEntity.getUser_sex());
    }

    @Override
    public void returnUserPhone(String data) {
        ToastUtil.showShort(this,"修改手机号成功:"+data);
    }

    @Override
    public void returnUserCard(String msg) {

    }

    @Override
    public void returnUserPlateNumber(String user_carid) {
        ToastUtil.showShort(this,"修改车牌号成功:"+user_carid);
    }

    @Override
    public void returnUserQuit(String msg) {

    }

}
